import { Component, OnInit, Output, EventEmitter, ViewChild } from '@angular/core';
import { Service } from '../../../server/server';
import { tablestructureDatas,model, tables } from '../database.model';
import { from } from 'rxjs';
import { Console } from '@angular/core/src/console';
import { DatabaseService } from '../database.service';
import { terminal } from 'app/system/terminal/terminal.data';
import { timeout } from 'q';

declare var $: any;
@Component({
  selector: 'app-database-add',
  templateUrl: './database-add.component.html'
})
export class DatabaseAddComponent implements OnInit {

  constructor(private server: Service, private databaseService: DatabaseService) { }

  ngOnInit() {
    this.initData();
    this.model = {
      name:"",
      id:"",
      state:0
    }
    // this.tables={
    //   tables:{tableDesc:'',tableName:'',tableId:''}
    // }

    // if(this.databaseService.typeLength.indexOf (this.terminals[0].columeType) >= 0){
    //   this.terminals[0].columeLengthtf = 1;
    // }else{
    //   this.terminals[0].columeLengthtf = 0;
    // }
  
    //接收详情数据
    
    this.databaseupdateeventEmit = this.server.databaseupdateeventEmit.subscribe((value: any) => {
      this.tables = value;
      //console.log(this.terminals)
      this.terminals = this.tables.tablesColRelations;
      for (let i in this.terminals) {
        this.terminals[i].columeOrder =+ i+1;
      }
      this.model = {name:"修改表",id:"update",state:2};
      setTimeout(() => {
        $("#update").modal("show");
      }, 100);
      
    });
    //add
    this.databaseAddeventEmit = this.databaseService.databaseAddeventEmit.subscribe((value: any) => {
      this.model = {name:"创建表",id:"add",state:1}
    });
  }

  //取消订阅
  public databaseupdateeventEmit:any;
  public databaseAddeventEmit:any;
  ngOnDestroy() {
    this.databaseupdateeventEmit ? this.databaseupdateeventEmit.unsubscribe() : '';
    this.databaseupdateeventEmit ? this.databaseupdateeventEmit.unsubscribe() : '';
  }

  ngDoCheck(){
    
  }

  public columeName:any;
  public model:model;
  public tables: any ={tables:{tableDesc:'',tableName:'',tableId:''}};
  public terminals: any;
  public typelist: any;


  //刷新
  @Output()
  addOutput: EventEmitter<any> = new EventEmitter();

  //清空
  @ViewChild('newlyAddedForm') formElement: HTMLFormElement;
  reset() {
    $("#add").modal("hide");
    $("#update").modal("hide");
    this.formElement.reset();
    this.databaseService.initData();
    this.initData();
  }

  submitNewlyAdded(val) {
    //console.log(val)
    let tables = { tableDesc: this.tables.tables.tableDesc, tableName: this.tables.tables.tableName, tableId: val.tableId }
    this.mainArray();
    //console.log(tables)
    //console.log(this.databaseService.terminals)

    this.databaseService.access(this.model.state, val.tableId || '', tables, this.terminals).then((res: any) => {
      this.addOutput.emit(res.result_code);
      if (res.result_code == '0000') {
        //prompt
        this.server.prompt = { prompt: true, success: true, fail: false, promptStatement: res.message };
        this.server.promptclose;
        this.reset()
      } else {
        this.server.prompt = { prompt: true, success: false, fail: true, promptStatement: res.message };
        this.server.promptclose;
      }
    })
 
    
  }

  //数据初始化
  public initData(){
    this.terminals = this.databaseService.terminals;
    this.typelist = this.databaseService.typeDate();
  }
  

  //上移下移
  public upDown(val, i, terminals) {
    if (val == 0) {
      //up
      if (i == 0) {
        return;
      }
      this.swapItems(terminals, i, i - 1);
    } else {
      //down
      if (i == terminals.length - 1) {
        return;
      }
      this.swapItems(terminals, i, i + 1);
    }
    for (let i in this.terminals) {
      this.terminals[i].columeOrder =+ i+1;
    }
  }
  public swapItems(arr, index1, index2) {
    arr[index1] = arr.splice(index2, 1, arr[index1])[0];
    return arr;
  }

  //添加一行
  addTr() {
    // this.databaseService.addTr();
    // this.terminals = this.databaseService.terminals;
    
    this.terminals = [...this.terminals, this.databaseService.getTerminalTemplate()];
    for (let i in this.terminals) {
      this.terminals[i].columeOrder =+ i+1;
      if(this.terminals[i].columeName == ''){
        this.columeNametf = false;
      }
    }
    this.modalAuto();
    
  }

  

  //滑动块类型转换
  public mainArray() {
    for (let i in this.terminals) {
      this.terminals[i].columeNotNull = Number(this.terminals[i].columeNotNull);
      this.terminals[i].primaryKey = Number(this.terminals[i].primaryKey);
    }
  }

  //主键单选
  // public primaryKeymin(i) {
  //   this.databaseService.primaryKeymin(i);
  // }
  //主键单选
  public primaryKeymin(i) {
    for (let k in this.terminals) {
      this.terminals[k].primaryKey = false;
    }
    if(this.terminals){
      this.terminals[i].primaryKey = true;
    }
    
  }

  //删除一行
  deletetr(i) {
    this.modalAuto();
    if (this.terminals.length == 1) {
      this.server.prompt = { prompt: true, success: false, fail: false, promptStatement: "最后一行了" };
      this.server.promptclose;
    } else {
      this.terminals.splice(i, 1);
    }
  }

  public modalAuto(){
    setTimeout(() => {
      this.server.modalHeightAuto();
    }, 300);
  }

  
  //控制输入
  
  typeChange(event, item, i) {
    item.columeType = event.target.value;
    let type = this.terminals[i].columeType;
    //小数点
    if (this.databaseService.typeXsd.indexOf(type) >= 0) {
      this.terminals[i].columeXsdtf = 1;
      this.terminals[i].columeXsd = null;
      this.terminals[i].columeLengthtf = 1;
    } else{
      this.terminals[i].columeXsd = 0;
      //长度
      if(this.databaseService.typeLength.indexOf(type) >= 0){
        this.terminals[i].columeLengthtf = 1;
        this.terminals[i].columeXsdtf = 0;
      }else{
        this.terminals[i].columeLengthtf = 0;
        this.terminals[i].columeXsdtf = 0;
      }
    }

    //console.log(this.terminals)
    
  }

  //验证列名
  public columeNametf:boolean = false;
  nameChange(event, item) {
    item.columeName = event.target.value;
    let reg = /^(?![0-9]*$)[a-zA-Z0-9_]{1,31}$/;
    //console.log(item.columeName,reg.test(item.columeName))
    if(reg.test(item.columeName)){
      this.columeNametf = true;
    }else{
      this.columeNametf = false;
    }
  }
  lengthChange(event, item) {
    item.columeLength = event.target.value;
  }
  xsdChange(event, item) {
    item.columeXsd = event.target.value;
  }
  // notnullChange(event, item) {
  //   item.columeNotNull = event.target.value;
  // }
  orderChange(event, item,i) {
    item.columeOrder = i+1;
    //console.log(item)
  }
  // defaultChange(event, item){
  //   event.target.value = item.columeDefault;
  // }
}


